<template>
  <div class="box">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/page' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <div style="margin-top: 15px">
        <el-input
          style="width: 20%"
          placeholder="请输入内容"
          v-model="keyword"
          class="input-with-select"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            class="aa"
            @click="getlist()"
          ></el-button>
        </el-input>
        <el-button
          style="margin-left: 10px"
          type="primary"
          @click="dialogVisible = true"
          >添加用户</el-button
        >
      </div>
      <template>
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column type="index" label="#" width="180">
          </el-table-column>
          <el-table-column prop="username" label="用户名" width="180">
          </el-table-column>
          <el-table-column prop="email" label="邮箱"> </el-table-column>
          <el-table-column prop="mobile" label="电话"> </el-table-column>
          <el-table-column prop="role_name" label="角色"> </el-table-column>
          <el-table-column prop="address" label="状态">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.mg_state"
                active-color="#13ce66"
                inactive-color="#ccc"
                @change="swit(scope.row)"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)"
              ></el-button>
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete(scope.row.id)"
              ></el-button>
              <el-tooltip
                class="item"
                effect="dark"
                content="分配角色"
                placement="top-start"
              >
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-s-tools"
                  @click="jiao(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagenum"
        :page-sizes="[2, 4, 6, 8]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 模态框 -->
    <el-dialog title="添加用户" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="ruleForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
    <!-- 模态框 -->
    <!-- 编辑 -->
    <el-dialog title="修改用户" :visible.sync="dialog" width="30%">
      <el-form
        :model="rule"
        :rules="rules"
        ref="rule"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input disabled v-model="rule.username"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="rule.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="rule.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rese('rule')">取 消</el-button>
        <el-button type="primary" @click="submi('rule')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑 -->
    <!-- 分配角色 -->
    <el-dialog title="分配角色" :visible.sync="Visible" width="40%">
      <el-form>
        <el-form-item label="当前用户:">
          {{ role.username }}
        </el-form-item>
        <el-form-item label="当前角色:">
          {{ role.rolename }}
        </el-form-item>
        <el-form-item label="分配新的角色">
          <el-select
            v-model="Value"
            placeholder="请选择活动区域"
            @change="sele"
          >
            <el-option
              v-for="(item, index) in arr"
              :key="index"
              :label="item.roleName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="Visible = false">取 消</el-button>
        <el-button type="primary" @click="app">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 分配角色 -->
  </div>
</template>

<script>
import { users, adduse, Delete, switc, bian, role, roles } from '@/api/login.js'

export default {
  data() {
    return {
      keyword: '',
      pagenum: 1,
      pagesize: 2,
      tableData: [],
      dialog: false,
      total: 0,
      dialogVisible: false,
      Visible: false,
      ruleForm: {
        username: '',
        password: '',
        email: '',
        mobile: '',
      },
      rule: {
        username: '',
        email: '',
        mobile: '',
      },
      role: {
        username: '',
        rolename: '',
      },
      Value: '',
      // 角色数组
      arr: [],
      // 角色ID
      role_id: 0,
      // 用户id
      usem_id: 0,
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6 到 16 个字符',
            trigger: 'blur',
          },
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {
            min: 6,
            max: 16,
            message: '长度在 6到 16 个字符',
            trigger: 'blur',
          },
        ],
        mobile: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          {
            min: 6,
            max: 11,
            message: '长度在 6 到 11 个字符',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  mounted() {
    this.getlist()
  },
  methods: {
    // 列表渲染
    getlist() {
      users({
        query: this.keyword,
        pagenum: this.pagenum,
        pagesize: this.pagesize,
      }).then((res) => {
        // console.log(res.data.data.total)
        this.total = res.data.data.total
        this.tableData = res.data.data.users
        if (res.data.meta.status == 200) {
          this.$message({
            type: 'success',
            message: res.data.meta.msg,
          })
        }
      })
    },
    // 分页的实现
    handleSizeChange(siae) {
      // console.log(siae)
      this.pagesize = siae
      this.getlist()
    },
    // 分页的实现
    handleCurrentChange(page) {
      // console.log(page)
      this.pagenum = page
      this.getlist()
    },
    // 添加
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          adduse(this.ruleForm).then((res) => {
            // console.log(res)
            if (res.data.meta.status == 200) {
              this.$message({
                type: 'success',
                message: res.data.meta.msg,
              })
            }
          })
          this.getlist()
          this.dialogVisible = false
          this.ruleForm = {
            username: '',
            password: '',
            email: '',
            mobile: '',
          }
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    // 取消
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.dialogVisible = false
    },
    // 删除
    handleDelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          Delete(id).then((res) => {
            // console.log(res)
            if (res.data.meta.status == 200) {
              this.$message({
                message: res.data.meta.msg,
                type: 'success',
              })
              this.getlist()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除',
          })
        })
    },
    // 修改状态
    swit(e) {
      let id = e.id
      let aa = e.mg_state
      let url = `users/${id}/state/${aa}`
      // console.log(url)
      switc(url).then((res) => {
        if (res.data.meta.status == 200) {
          this.$message({
            type: 'success',
            message: res.data.meta.msg,
          })
          this.getlist()
        }
      })
    },
    // 编辑
    handleEdit(row) {
      // console.log(row)
      this.dialog = true
      this.rule = {
        id: row.id,
        username: row.username,
        email: row.email,
        mobile: row.mobile,
      }
    },
    //修改
    submi(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let obj = {
            id: this.rule.id,
            email: this.rule.email,
            mobile: this.rule.mobile,
          }
          bian(obj).then((res) => {
            this.$message({
              message: res.data.meta.msg,
              type: 'success',
            })
            this.getlist()
            this.dialog = false
          })
        } else {
          // console.log('error submit!!')
          return false
        }
      })
    },
    rese(formName) {
      this.$refs[formName].resetFields()
      this.dialog = false
    },
    jiao(e) {
      // console.log(e)
      this.role = {
        username: e.username,
        rolename: e.role_name,
      }
      this.Visible = true
      this.usem_id = e.id
      role().then((res) => {
        // console.log(res.data.data)
        this.arr = res.data.data
      })
    },
    sele(e) {
      // console.log(e)
      this.role_id = e
    },
    app() {
      let obj = {
        id: this.usem_id,
        rid: this.role_id,
      }
      roles(obj).then((res) => {
        // console.log(res)
        if (res.data.meta.status == 200) {
          this.$message({
            type: 'success',
            message: res.data.meta.msg,
          })
          this.getlist()
          this.Visible = false
        }
      })
    },
  },
  computed: {},
  watch: {},
}
</script>

<style scoped lang="scss">
.box {
  padding: 20px;
  .box-card {
    margin-top: 30px;
  }
}
.el-breadcrumb {
  font-size: 20px;
}
.aa {
  text-align: center;
}
</style>
